{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "ROOT_PATH = os.path.dirname(os.path.abspath(\".\"))\n",
    "sys.path.append(ROOT_PATH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d:\\\\Code\\\\Project\\\\CB-GPT'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ROOT_PATH"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "from store import MySQL"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "mysql = MySQL()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"select * from user where id = %s\"\n",
    "params = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = mysql.select(sql, params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1,\n",
       " [{'id': 1,\n",
       "   'username': '17879386605',\n",
       "   'password': 's1807931',\n",
       "   'nickname': 'esheeper',\n",
       "   'avatar': '2024-03/esheeper.png',\n",
       "   'createAt': datetime.datetime(2024, 3, 16, 11, 24, 11),\n",
       "   'updateAt': datetime.datetime(2024, 3, 16, 11, 24, 11)}])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "username = \"1787938661\"\n",
    "usernames = [username + str(i) for i in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['17879386610',\n",
       " '17879386611',\n",
       " '17879386612',\n",
       " '17879386613',\n",
       " '17879386614',\n",
       " '17879386615',\n",
       " '17879386616',\n",
       " '17879386617',\n",
       " '17879386618',\n",
       " '17879386619']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "usernames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "password = \"123456\"\n",
    "nicknames = [\"esheeper\" + str(i) for i in range(10)]\n",
    "avatar = \"2024-03/esheeper.png\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [[usernames[i], password, nicknames[i], avatar] for i in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['17879386610', '123456', 'esheeper0', '2024-03/esheeper.png'],\n",
       " ['17879386611', '123456', 'esheeper1', '2024-03/esheeper.png'],\n",
       " ['17879386612', '123456', 'esheeper2', '2024-03/esheeper.png'],\n",
       " ['17879386613', '123456', 'esheeper3', '2024-03/esheeper.png'],\n",
       " ['17879386614', '123456', 'esheeper4', '2024-03/esheeper.png'],\n",
       " ['17879386615', '123456', 'esheeper5', '2024-03/esheeper.png'],\n",
       " ['17879386616', '123456', 'esheeper6', '2024-03/esheeper.png'],\n",
       " ['17879386617', '123456', 'esheeper7', '2024-03/esheeper.png'],\n",
       " ['17879386618', '123456', 'esheeper8', '2024-03/esheeper.png'],\n",
       " ['17879386619', '123456', 'esheeper9', '2024-03/esheeper.png']]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "insertSql = \"insert into user(username, password, nickname, avatar) values (%s, %s ,%s, %s)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import pymysql\n",
    "# #打开数据库连接\n",
    "# db = pymysql.connect(\n",
    "#     host = '192.168.118.129',   #服务端地址\n",
    "#     port = 3306,    #服务端端口\n",
    "#     user = 'root',   #用户名\t\t\n",
    "#     password = '123456',   #密码\n",
    "#     database = 'cbgpt',   #要连接的数据库\n",
    "# )\n",
    "\n",
    "# #使用 cursor() 方法创建一个游标对象 cursor\n",
    "# cursor = db.cursor(pymysql.cursors.DictCursor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#res = cursor.execute(insertSql, [\"1820138420\", \"123456\", \"edog\", \"test\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(cursor.lastrowid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#res = cursor.executemany(insertSql, data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(cursor.lastrowid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#print(cursor.fetchall())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#db.insert_id()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "#db.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#cursor.execute(\"select last_insert_id();\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "#insert_id_info = cursor.fetchall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "#insert_id_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<data_store.MySQL at 0x28bc7b6e250>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mysql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<pymysql.cursors.DictCursor object at 0x0000028BC7B052D0>\n"
     ]
    }
   ],
   "source": [
    "res = mysql.insertMany(insertSql, data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[33, 34, 35, 36, 37, 38, 39, 40, 41, 42]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
